package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tianxia.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/*
 * 天下信用报告记录-股权在职
 *
 * @author:szw
 * */

public class TxCreditGuQuanParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 天下信用报告记录-股权在职
     *
     * @param context
     * @param model
     * @param successful
     * @return boolean
     * @throws IOException
     * @throws InterruptedException
     * @author typole
     * @date 2020/12/01 5:08 下午
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TxCreditReportRecord obj = (TxCreditReportRecord) model;
        TxCreditReportData data = obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);

        String shareholderInfo = data.getShareholderInfo();
        if (shareholderInfo != null) {
            JSONObject shareholderInfoJson = JSON.parseObject(shareholderInfo);
            //判断类型
            if (shareholderInfoJson.get("data") instanceof JSONArray) {
                JSONArray dataList = shareholderInfoJson.getJSONArray("data");
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject dataInfo = dataList.getJSONObject(i);

                    TxSb.delete(0, TxSb.length());
                    //公司名称
                    String corpName = dataInfo.getString("corpName");
                    TxSb.append(corpName == null ? "" : corpName).append(CRT001);
                    //工商注册号
                    String registNo = dataInfo.getString("registNo");
                    TxSb.append(registNo == null ? "" : registNo).append(CRT001);
                    //企业类型
                    String corpTypeDesc = dataInfo.getString("corpTypeDesc");
                    TxSb.append(corpTypeDesc == null ? "" : corpTypeDesc).append(CRT001);
                    //注册资本
                    String registFund = dataInfo.getString("registFund");
                    TxSb.append(registFund == null ? "" : registFund).append(CRT001);
                    //注册资本币种
                    String registCurrencyDesc = dataInfo.getString("registCurrencyDesc");
                    TxSb.append(registCurrencyDesc == null ? "" : registCurrencyDesc).append(CRT001);
                    //认缴出资额
                    String contributiveFund = dataInfo.getString("contributiveFund");
                    TxSb.append(contributiveFund == null ? "" : contributiveFund).append(CRT001);
                    //出资比例
                    String contributivePercent = dataInfo.getString("contributivePercent");
                    TxSb.append(contributivePercent == null ? "" : contributivePercent).append(CRT001);
                    //认缴出资额币种描述
                    String contributiveCurrencyDesc = dataInfo.getString("contributiveCurrencyDesc");
                    TxSb.append(contributiveCurrencyDesc == null ? "" : contributiveCurrencyDesc).append(CRT001);
                    //是否当前工
                    TxSb.append(CRT001);
                    //职称名称
                    TxSb.append(CRT001);
                    //工作年限
                    TxSb.append(CRT001);
                    TxSb.append("shareholderInfo").append(CRT001);
                    //-------------------------------------------------------------------------------新增字段
                    //机构类型
                    TxSb.append(CRT001);
                    //机构地址
                    TxSb.append(CRT001);
                    //企业类型（规模）
                    TxSb.append(CRT001);
                    //城市分类
                    TxSb.append(CRT001);
                    //所属行业
                    TxSb.append(CRT001);
                    //行业代码
                    TxSb.append(CRT001);
                    //行业排名
                    TxSb.append(CRT001);
                    //持股比例
                    TxSb.append(CRT001);
                    //持有价值
                    TxSb.append(CRT001);
                    //企业标签
                    TxSb.append(CRT001);
                    //当前估值/当前市值
                    TxSb.append(CRT001);
                    //estimate字段含义
                    TxSb.append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }


            } else {
                JSONObject shareholderData = shareholderInfoJson.getJSONObject("data");
                JSONArray dataListData = shareholderData != null ? shareholderData.getJSONArray("list") : new JSONArray();
                for (int i = 0; i < dataListData.size(); i++) {
                    JSONObject dataInfo = dataListData.getJSONObject(i);
                    TxSb.delete(0, TxSb.length());
                    //公司名称
                    String corpName = dataInfo.getString("corpName");
                    TxSb.append(corpName == null ? "" : corpName).append(CRT001);
                    //工商注册号
                    String registNo = dataInfo.getString("regNo");
                    TxSb.append(registNo == null ? "" : registNo).append(CRT001);
                    //企业类型
                    String corpTypeDesc = dataInfo.getString("corpTypeCaption");
                    TxSb.append(corpTypeDesc == null ? "" : corpTypeDesc).append(CRT001);
                    //注册资本
                    String registFund = dataInfo.getString("registFund");
                    TxSb.append(registFund == null ? "" : registFund).append(CRT001);
                    //注册资本币种
                    TxSb.append(CRT001);
                    //认缴出资额
                    String contributiveFund = dataInfo.getString("contributiveFund");
                    TxSb.append(contributiveFund == null ? "" : contributiveFund).append(CRT001);
                    //出资比例
                    TxSb.append(CRT001);
                    //认缴出资额币种描述
                    TxSb.append(CRT001);
                    //是否当前工
                    TxSb.append(CRT001);
                    //职称名称
                    TxSb.append(CRT001);
                    //工作年限
                    TxSb.append(CRT001);
                    TxSb.append("shareholderInfo").append(CRT001);
                    //-------------------------------------------------------------------------------新增字段
                    //机构类型
                    String type = dataInfo.getString("type");
                    TxSb.append(type == null ? "" : type).append(CRT001);
                    //机构地址
                    String address = dataInfo.getString("address");
                    TxSb.append(address == null ? "" : address).append(CRT001);
                    //企业类型（规模）
                    String corpType = dataInfo.getString("corpType");
                    TxSb.append(corpType == null ? "" : corpType).append(CRT001);
                    //城市分类
                    String cityCategory = dataInfo.getString("cityCategory");
                    TxSb.append(cityCategory == null ? "" : cityCategory).append(CRT001);
                    //所属行业
                    String trade = dataInfo.getString("trade");
                    TxSb.append(trade == null ? "" : trade).append(CRT001);
                    //行业代码
                    String tradeCode = dataInfo.getString("tradeCode");
                    TxSb.append(tradeCode == null ? "" : tradeCode).append(CRT001);
                    //行业排名
                    String tradeRanking = dataInfo.getString("tradeRanking");
                    TxSb.append(tradeRanking == null ? "" : tradeRanking).append(CRT001);
                    //持股比例
                    String holdRatio = dataInfo.getString("holdRatio");
                    TxSb.append(holdRatio == null ? "" : holdRatio).append(CRT001);
                    //持有价值
                    String holdValue = dataInfo.getString("holdValue");
                    TxSb.append(holdValue == null ? "" : holdValue).append(CRT001);
                    //企业标签
                    String corpLabel = dataInfo.getString("corpLabel");
                    TxSb.append(corpLabel == null ? "" : corpLabel).append(CRT001);
                    //当前估值/当前市值
                    String estimate = dataInfo.getString("estimate");
                    TxSb.append(estimate == null ? "" : estimate).append(CRT001);
                    //estimate字段含义
                    String valuationOrMarketValue = dataInfo.getString("valuationOrMarketValue");
                    TxSb.append(valuationOrMarketValue == null ? "" : valuationOrMarketValue).append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }

            }
        }

        WorkUnitInfo workUnitInfo = data.getWorkUnitInfo();
        if (workUnitInfo != null) {
            List<WorkUnitInfoData> wu = workUnitInfo.getData() == null ? new ArrayList<>() : workUnitInfo.getData();
            if (!wu.isEmpty()) {
                for (WorkUnitInfoData w : wu) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(w.getCorpName() == null ? "" : w.getCorpName()).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(w.getIsCurrent() == null ? "" : w.getIsCurrent()).append(CRT001);
                    TxSb.append(w.getPositionCaption() == null ? "" : w.getPositionCaption()).append(CRT001);
                    TxSb.append(w.getWorkingLife() == null ? "" : w.getWorkingLife()).append(CRT001);
                    TxSb.append("workUnitInfo").append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }

        LegalPersonInfo legalPersonInfo = data.getLegalPersonInfo();
        if (legalPersonInfo != null) {
            List<LegalPersonInfoData> lp = legalPersonInfo.getData() == null ? new ArrayList<>() : legalPersonInfo.getData();
            if (!lp.isEmpty()) {
                for (LegalPersonInfoData l : lp) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(l.getCorpName() == null ? "" : l.getCorpName()).append(CRT001);
                    TxSb.append(l.getRegistNo() == null ? "" : l.getRegistNo()).append(CRT001);
                    TxSb.append(l.getCorpTypeDesc() == null ? "" : l.getCorpTypeDesc()).append(CRT001);
                    TxSb.append(l.getRegistFund() == null ? "" : l.getRegistFund()).append(CRT001);
                    TxSb.append(l.getRegistCurrencyDesc() == null ? "" : l.getRegistCurrencyDesc()).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append("legalPersonInfo").append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }

        TopManagerInfo topManagerInfo = data.getTopManagerInfo();
        if (topManagerInfo != null) {
            List<TopManagerInfoData> tm = topManagerInfo.getData() == null ? new ArrayList<>() : topManagerInfo.getData();
            if (!tm.isEmpty()) {
                for (TopManagerInfoData t : tm) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(t.getCorpName() == null ? "" : t.getCorpName()).append(CRT001);
                    TxSb.append(t.getRegistNo() == null ? "" : t.getRegistNo()).append(CRT001);
                    TxSb.append(t.getCorpTypeDesc() == null ? "" : t.getCorpTypeDesc()).append(CRT001);
                    TxSb.append(t.getRegistFund() == null ? "" : t.getRegistFund()).append(CRT001);
                    TxSb.append(t.getRegistCurrencyDesc() == null ? "" : t.getRegistCurrencyDesc()).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(t.getPositionDesc() == null ? "" : t.getPositionDesc()).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append("topManagerInfo").append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }
        return shareholderInfo != null || workUnitInfo != null || legalPersonInfo != null || topManagerInfo != null;
    }

    @Override
    public String getSubType() {
        return ".guquan";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tx_credit_guquan_inc";
    }

    @Override
    public String getType() {
        return RecordType.TX_CREDIT_REPORT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "天下信用-股权在职";
    }
}
